package com.coollang.smartbra.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.base.MyApplication;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/18 0018.
 */

public class RunSetDialog extends Dialog implements View.OnClickListener {

    private final View view;
    @BindView(R.id.ll_autostop)
    LinearLayout llAutostop;
    @BindView(R.id.tv_autostop)
    TextView tvAutostop;
    private boolean showAutoStop;
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.sound)
    ImageView sound;
    @BindView(R.id.auto_stop)
    ImageView autoStop;
    @BindView(R.id.screen_light)
    ImageView screenLight;
    @BindView(R.id.screen_lock)
    ImageView screenLock;
    private Context mContext;

    public RunSetDialog(Context context, boolean showAutoStop,View view) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.showAutoStop = showAutoStop;
        this.view = view;
    }

    @Override
    public void show() {
        Window window = this.getWindow();
        window.setWindowAnimations(R.style.dialog_enter_exit); // 添加动画
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        window.getDecorView().setPadding(0, 0, 0, 0);
        // 以下这两句是为了保证按钮可以水平满屏
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        // 设置显示位置
        this.onWindowAttributesChanged(lp);
        window.setGravity(Gravity.BOTTOM);// 居底
        // 设置点击外围解散
        this.setCanceledOnTouchOutside(true);
        super.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_runset);
        ButterKnife.bind(this);
        initView();
        setsetDisPlay();
    }

    private void setsetDisPlay() {
        if (MyApplication.isSound) {
            sound.setBackgroundResource(R.drawable.sound_on);
        } else {
            sound.setBackgroundResource(R.drawable.sound_off);
        }
        if (MyApplication.isStop) {
            autoStop.setBackgroundResource(R.drawable.stop_on);
        } else {
            autoStop.setBackgroundResource(R.drawable.stop_off);
        }
        if (MyApplication.isLight) {
            screenLight.setBackgroundResource(R.drawable.screen_light_on);
            view.setKeepScreenOn(true);
        } else {
            screenLight.setBackgroundResource(R.drawable.screen_light_off);
            view.setKeepScreenOn(false);
        }
        if (MyApplication.isLock) {
            screenLock.setBackgroundResource(R.drawable.screen_light_on);
        } else {
//            PowerManagerUtils.unLockScreen();
            screenLock.setBackgroundResource(R.drawable.screen_light_off);
        }
    }

    private void initView() {
        if (!showAutoStop) {
            llAutostop.setVisibility(View.GONE);
            tvAutostop.setVisibility(View.GONE);
        }
        sound.setOnClickListener(this);
        autoStop.setOnClickListener(this);
        screenLight.setOnClickListener(this);
        screenLock.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                this.dismiss();
                break;
            case R.id.sound:
                setSoundBG();
                break;
            case R.id.auto_stop:
                setStopBG();
                break;
            case R.id.screen_light:
                setLightBG();
                break;
            case R.id.screen_lock:
                setLockBG();
                break;
            default:
                break;
        }
    }

    private void setSoundBG() {
        if (MyApplication.isSound) {
            MyApplication.isSound = false;
            sound.setBackgroundResource(R.drawable.sound_off);
        } else {
            MyApplication.isSound = true;
            sound.setBackgroundResource(R.drawable.sound_on);
        }
    }

    private void setStopBG() {
        if (MyApplication.isStop) {
            MyApplication.isStop = false;
            autoStop.setBackgroundResource(R.drawable.stop_off);
        } else {
            MyApplication.isStop = true;
            autoStop.setBackgroundResource(R.drawable.stop_on);
        }
    }

    private void setLightBG() {
        if (MyApplication.isLight) {
            MyApplication.isLight = false;
            screenLight.setBackgroundResource(R.drawable.screen_light_off);
            view.setKeepScreenOn(false);
        } else {
            MyApplication.isLight = true;
            screenLight.setBackgroundResource(R.drawable.screen_light_on);
            view.setKeepScreenOn(true);
        }
    }

    private void setLockBG() {
        if (MyApplication.isLock) {
            MyApplication.isLock = false;
            screenLock.setBackgroundResource(R.drawable.screen_light_off);
        } else {
            MyApplication.isLock = true;
            screenLock.setBackgroundResource(R.drawable.screen_light_on);
        }
    }
}
